package com.ruoyi.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

import com.ruoyi.domain.ZhwyHirer;
import com.ruoyi.domain.ZhwyShops;
import com.ruoyi.mapper.ZhwyHirerMapper;
import com.ruoyi.mapper.ZhwyShopsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.ZhwyRentalcontractMapper;
import com.ruoyi.domain.ZhwyRentalcontract;
import com.ruoyi.service.IZhwyRentalcontractService;

import static java.time.LocalTime.now;

/**
 * 租售合同Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-07
 */
@Service
public class ZhwyRentalcontractServiceImpl implements IZhwyRentalcontractService 
{
    @Autowired
    private ZhwyRentalcontractMapper zhwyRentalcontractMapper;

    @Autowired
    private ZhwyShopsMapper zhwyShopsMapper;

    @Autowired
    private ZhwyHirerMapper zhwyHirerMapper;

    /**
     * 查询租售合同
     * 
     * @param id 租售合同主键
     * @return 租售合同
     */
    @Override
    public ZhwyRentalcontract selectZhwyRentalcontractById(Long id)
    {
        ZhwyRentalcontract zhwyRentalcontract = zhwyRentalcontractMapper.selectZhwyRentalcontractById(id);
        ZhwyShops zhwyShops = zhwyShopsMapper.selectZhwyShopsById(zhwyRentalcontract.getShopsId());
        zhwyRentalcontract.setShopsCode(zhwyShops.getCode());
        ZhwyHirer zhwyHirer = zhwyHirerMapper.selectZhwyHirerById(zhwyRentalcontract.getHirerId());
        zhwyRentalcontract.setHirerCompany(zhwyHirer.getCompany());
        zhwyRentalcontract.setHirerName(zhwyHirer.getName());
        zhwyRentalcontract.setHirerSex(zhwyHirer.getSex());
        zhwyRentalcontract.setHirerCard(zhwyHirer.getCard());
        zhwyRentalcontract.setHirerPhone(zhwyHirer.getPhone());
        return zhwyRentalcontract;
    }
    /**
     * 查询租售合同列表
     * 
     * @param zhwyRentalcontract 租售合同
     * @return 租售合同
     */
    @Override
    public List<ZhwyRentalcontract> selectZhwyRentalcontractList(ZhwyRentalcontract zhwyRentalcontract)
    {
        List<ZhwyRentalcontract> list = zhwyRentalcontractMapper.selectZhwyRentalcontractList(zhwyRentalcontract);
        Iterator<ZhwyRentalcontract> iterator = list.iterator();
        while (iterator.hasNext()){
            ZhwyRentalcontract next = iterator.next();
            ZhwyShops zhwyShops = zhwyShopsMapper.selectZhwyShopsById(next.getShopsId());
            next.setShopsCode(zhwyShops.getCode());
            ZhwyHirer zhwyHirer = zhwyHirerMapper.selectZhwyHirerById(next.getHirerId());
            next.setHirerCompany(zhwyHirer.getCompany());
            next.setHirerName(zhwyHirer.getName());
            next.setHirerPhone(zhwyHirer.getPhone());
//            Date date = new Date();
//            if(next.getType()==0){
//                if(date.after(next.getEndTime())){
//                    next.setState(2L);
//                }
//            }
        }
        return list;
    }

    /**
     * 新增租售合同
     * 
     * @param zhwyRentalcontract 租售合同
     * @return 结果
     */
    @Override
    public int insertZhwyRentalcontract(ZhwyRentalcontract zhwyRentalcontract)
    {
        if(zhwyRentalcontract.getType()==0){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(zhwyRentalcontract.getStartTime()); // 设置日期
            calendar.add(Calendar.MONTH, Math.toIntExact(zhwyRentalcontract.getTaxi())); // 增加月份
            Date newDate = calendar.getTime(); // 获取新的日期
            System.out.println("最新日期"+newDate);
            zhwyRentalcontract.setEndTime(newDate);
            Date date = new Date();

            if(date.after(zhwyRentalcontract.getEndTime())){
                zhwyRentalcontract.setState(2L);
            }
        }


        return zhwyRentalcontractMapper.insertZhwyRentalcontract(zhwyRentalcontract);
    }
    /**
     * 修改租售合同
     * 
     * @param zhwyRentalcontract 租售合同
     * @return 结果
     */
    @Override
    public int updateZhwyRentalcontract(ZhwyRentalcontract zhwyRentalcontract)
    {
        if(zhwyRentalcontract.getType()==0){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(zhwyRentalcontract.getStartTime()); // 设置日期
            calendar.add(Calendar.MONTH, Math.toIntExact(zhwyRentalcontract.getTaxi())); // 增加月份
            Date newDate = calendar.getTime(); // 获取新的日期
            System.out.println("最新日期"+newDate);
            zhwyRentalcontract.setEndTime(newDate);
        }
        return zhwyRentalcontractMapper.updateZhwyRentalcontract(zhwyRentalcontract);
    }

    /**
     * 批量删除租售合同
     * 
     * @param ids 需要删除的租售合同主键
     * @return 结果
     */
    @Override
    public int deleteZhwyRentalcontractByIds(Long[] ids)
    {
        return zhwyRentalcontractMapper.deleteZhwyRentalcontractByIds(ids);
    }

    /**
     * 删除租售合同信息
     * 
     * @param id 租售合同主键
     * @return 结果
     */
    @Override
    public int deleteZhwyRentalcontractById(Long id)
    {
        return zhwyRentalcontractMapper.deleteZhwyRentalcontractById(id);
    }

    @Override
    public Map select(Long did, Long bid) {

//        List rcList = new ArrayList<>();
        Map map = new HashMap<>();
        List<Integer> floors = zhwyShopsMapper.selectDBid(did, bid);
        System.out.println("---------------"+floors.toString());
        map.put("key",floors.toArray());
        Map map1 = new HashMap<>();
        for (Integer floor : floors) {
            List<ZhwyRentalcontract> list = zhwyRentalcontractMapper.selectList();
            System.out.println("============"+list.toString());
            List<String> strings = zhwyShopsMapper.selectDBidAndFloor(did, bid, floor);
            System.out.println("-=-=-=--=-=="+strings.toString());
            Iterator<ZhwyRentalcontract> iterator = list.iterator();
            while (iterator.hasNext()){
                ZhwyRentalcontract zr = iterator.next();
                ZhwyShops zhwyShops = zhwyShopsMapper.selectZhwyShopsById(zr.getShopsId());
                System.out.println("对象为"+zhwyShops.toString());
                if(!strings.contains(zhwyShops.getCode())){
                    iterator.remove();
                }
                zr.setShopsCode(zhwyShops.getCode());
                zr.setShopsState(Math.toIntExact(zhwyShops.getState()));
                zr.setShopSUserArea(zhwyShops.getUseArea());
                ZhwyHirer zhwyHirer = zhwyHirerMapper.selectZhwyHirerById(zr.getHirerId());
                zr.setHirerCompany(zhwyHirer.getCompany());
                zr.setHirerName(zhwyHirer.getName());
                zr.setHirerPhone(zhwyHirer.getPhone());
                zr.setHirerCard(zhwyHirer.getCard());
                zr.setHirerSex(zhwyHirer.getSex());
            }
            System.out.println("1111111111111111111"+list.toString());
            map1.put(floor,list);
        }
        map.put("list",map1);
        return map;
    }

    @Override
    public int updateState(Long id) {
        int i = zhwyRentalcontractMapper.updateState(id);
        return i;
    }


}
